package com.geely.controller;

import com.alibaba.fastjson.JSONObject;
import com.geely.config.CorrelationData2;
import com.geely.config.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.n3r.idworker.Sid;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jms.JmsProperties;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author 占助
 * @date 2020/7/11 10:32
 * @description ${类的描述}
 * @Copyright © 吉利汽车
 * @Version: 1.0
 */
@RestController
@Api(value = "rabbitmq测试接口",tags = {"rabbitmq测试接口" })
public class SendMessageController {
    @Autowired
    private RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法
    @Autowired
    private Sid sid;

    @ApiOperation(value = "direct exchange直连型交换机",notes = "direct exchange直连型交换机",httpMethod = "GET")
    @GetMapping("/sendDirectMessage33")
    @ResponseBody
    public R sendDirectMessage33() {
        String messageId =sid.nextShort();
        String messageData = "test message hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange3", "TestDirectRouting3", map);
        return R.success();
    }

    @ApiOperation(value = "direct exchange直连型交换机",notes = "direct exchange直连型交换机",httpMethod = "GET")
    @GetMapping("/sendDirectMessage22")
    @ResponseBody
    public R sendDirectMessage22() {
        String messageId =sid.nextShort();
        String messageData = "test message hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting2", map);
        return R.success();
    }

    @ApiOperation(value = "测试消息自动过期",notes = "测试消息自动过期",httpMethod = "GET")
    @GetMapping("/testTTL")
    public String testTTL() {
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setExpiration("20000"); // 设置过期时间，单位：毫秒
        byte[] msgBytes = "测试消息自动过期".getBytes();
        Message message = new Message(msgBytes, messageProperties);
        CorrelationData2 correlationData = new CorrelationData2();
        correlationData.setId("1234");
        correlationData.setMsg("测试消息自动过期");
        correlationData.setDesc("能不能成功?");
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", message, correlationData);
        return "ok";
    }

    @ApiOperation(value = "direct exchange直连型交换机",notes = "direct exchange直连型交换机",httpMethod = "GET")
    @GetMapping("/sendDirectMessage")
    @ResponseBody
    public R sendDirectMessage() {
        String messageId =sid.nextShort();
        String messageData = "test message hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        CorrelationData correlationData = new CorrelationData();
        correlationData.setId("1234");
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map, correlationData);
        return R.success();
    }
    @ApiOperation(value = "Topic Exchange 主题交换机",notes = "Topic Exchange 主题交换机",httpMethod = "GET")
    @GetMapping("/sendDirectMessage1")
    public R sendDirectMessage1() {
        String messageId =sid.nextShort();
        String messageData = "message: M A N";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        //将消息携带绑定键值：topicExchange 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("topicExchange", "topic.man", map);
        return R.success();
    }
    @ApiOperation(value = "Topic Exchange 主题交换机",notes = "Topic Exchange 主题交换机",httpMethod = "GET")
    @GetMapping("/sendDirectMessage2")
    public R sendDirectMessage2() {
        String messageId =sid.nextShort();
        String messageData = "message: woman is all ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        //将消息携带绑定键值：topicExchange 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("topicExchange", "topic.woman", map);
        return R.success();
    }

    @ApiOperation(value = "测试消息自动过期-主题交换机-测试死信交换机",notes = "测试消息自动过期-主题交换机-测试死信交换机",httpMethod = "GET")
    @GetMapping("/testTTLTopic")
    public String testTTLTopic() {
        MessageProperties messageProperties = new MessageProperties();
//        messageProperties.setExpiration("10000"); // 设置过期时间，单位：毫秒  队列设置了死信队列过期时间，这里可以不用设置了
        messageProperties.setContentEncoding("UTF-8");
        messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);//数据持久化，就算mq服务器重启也不会丢失

        JSONObject js = new JSONObject();
        js.put("msg", "测试消息自动过期-主题交换机-测试死信交换机");
        js.put("type", new Random().nextDouble() * 10 > 5);
//        byte[] msgBytes = "{msg=测试消息自动过期-主题交换机}".getBytes();
        byte[] msgBytes = js.toJSONString().getBytes();
        Message message = new Message(msgBytes, messageProperties);
        CorrelationData2 correlationData = new CorrelationData2();
        correlationData.setId("1234");
        correlationData.setMsg(js.getString("msg"));
        correlationData.setDesc(js.getString("type"));
        rabbitTemplate.convertAndSend("topicExchange", "topic.queue", message, correlationData);
        return "ok";
    }


    @ApiOperation(value = "Fanout Exchang 扇型交换机",notes = "Fanout Exchang 扇型交换机",httpMethod = "GET")
    @GetMapping("/sendFanoutMessage")
    public R sendFanoutMessage() {
        String messageId =sid.nextShort();
        String messageData = "message: testFanoutMessage  ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        //将消息携带绑定键值：topicExchange 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("fanoutExchange", null, map);
        return R.success();
    }
    @ApiOperation(value = "消息推送到server，但是在server里找不到交换机",notes = "消息推送到server，但是在server里找不到交换机",httpMethod = "GET")
    @GetMapping("/TestMessageAck")
    public R TestMessageAck() {
        String messageId =sid.nextShort();
        String messageData = "message: non-existent-exchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("non-existent-exchange", "TestDirectRouting", map);
        return R.success();
    }

    @ApiOperation(value = "消息推送到server，找到交换机了，但是没找到队列 ",notes = "消息推送到server，找到交换机了，但是没找到队列 ",httpMethod = "GET")
    @GetMapping("/TestMessageAck2")
    public R TestMessageAck2() {
        String messageId =sid.nextShort();
        String messageData = "message: lonelyDirectExchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("lonelyDirectExchange", "TestDirectRouting", map);
        return R.success();
    }


}
